﻿///
/// Pamela.Logic.Imps
///
namespace Pamela.Logic.Imps.Mvc
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Reflection;
    using System.Text;

    using Pamela.Logic.Helpers;
    using Pamela.Logic.Imps.Base;    
    using Pamela.Logic.Properties;

    /// <summary>
    /// ServiceInterfaceBuilder class
    /// </summary>
    internal class MvcServiceInterfaceBuilder : BaseBuilder
    {
        /// <summary>
        /// Initializes a new instance of the ServiceInterfaceBuilder class
        /// </summary>
        /// <param name="assembly">The shared DLL assembly</param>
        /// <param name="info">Builder  object containing all the info to necessary perform the scaffolding operation</param>
        public MvcServiceInterfaceBuilder(Assembly assembly, BuilderInfo info, string layerName)
            : base(assembly, info, layerName, "serviceinterface.template")
        {
        }

        /// <summary>
        /// Builds the service interface file based on the BuilderInfo provided
        /// </summary>
        public override void Build()
        {
            var target = string.Format("{0}\\{1}.Business\\Interfaces\\I{2}Service.cs", this.builderInfo.SolutionInnerDirectory, this.builderInfo.RootNamespace, this.builderInfo.SelectedModel);

            using (StreamReader template = new StreamReader(this.templatePath))
            using (StreamWriter output = new StreamWriter(target))
            {
                while (template.Peek() >= 0)
                {
                    string buffer = template.ReadLine();

                    buffer = buffer.Replace(Resources.NamespaceBal, this.builderInfo.BusinessLayerNamespace);
                    buffer = buffer.Replace(Resources.NamespaceDal, this.builderInfo.DataLayerNamespace);
                    buffer = buffer.Replace(Resources.Namespace, this.builderInfo.RootNamespace);
                    buffer = buffer.Replace(Resources.SelectedModel, this.builderInfo.SelectedModel);
                    buffer = buffer.Replace(Resources.Entity, pluralizationService.Pluralize(this.builderInfo.SelectedModel));
                    buffer = buffer.Replace(Resources.Context, this.builderInfo.DatabaseContext);

                    output.WriteLine(buffer);
                }
            }
        }
    }
}
